package com.diyunapp.xilinapp.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AccountSafeActivity extends StatusBarFragmentActivity {

    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.lin_email)
    LinearLayout linEmail;
    @Bind(R.id.lin_phone)
    LinearLayout linPhone;
    @Bind(R.id.lin_login_pass)
    LinearLayout linLoginPass;
    @Bind(R.id.recharge_pass)
    LinearLayout rechargePass;
    private View headView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_safe);
        ButterKnife.bind(this);
        headView = findViewById(R.id.accountsafe_menubar_inc);
        super.MenuWidget(headView);
    }
    @OnClick({R.id.go_back, R.id.lin_email, R.id.lin_phone,R.id.lin_login_pass,R.id.recharge_pass})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.go_back:
                    finish();
                break;
            case R.id.lin_email:
                getBindData();
//                nextAct(EmailCer1Activity.class);
                break;
            case R.id.lin_phone:
                getBindData1();
//                nextAct(PhoneCer1Activity.class);
                break;
            case R.id.lin_login_pass:
                nextAct(LoginPassActivity.class);
                break;
            case R.id.recharge_pass:
                nextAct(RechargePassActivity.class);
                break;
        }
    }
    private void getBindData() {
        String url=""+MainActivity.url+"act=goods&op=find_mobile_email&member_id="+ MainActivity.myLogin.getMemberid()+"";
//        String url = "http://www.xilingou.com/mobile/index.php?act=goods&op=find_mobile_email&member_id="+id+"";
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String resultinfo = responseInfo.result;
//                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");
//                            toast(String.valueOf(result));
                            if (result == 200) {
                                JSONObject recordObject = resultObject.optJSONObject("data");
                                String member_email = recordObject.optString("member_email");
                                String member_mobile = recordObject.optString("member_mobile");
                                int member_email_bind = recordObject.optInt("member_email_bind");
                                int member_mobile_bind = recordObject.optInt("member_mobile_bind");
                                if(member_email_bind==1){
                                    Intent intent2 = new Intent();
                                    intent2.setClass(AccountSafeActivity.this, EmailCer2Activity.class);
                                    intent2.putExtra("num",member_email);
                                    intent2.putExtra("type","邮箱");
                                    startActivity(intent2);
                                }else if(member_email_bind==0) {
                                    nextAct(EmailCerActivity.class);
                                }


                            } else {
                                toast("当前暂无数据");
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {
                        toast("请求失败");
                    }
                });
    }
    private void getBindData1() {
        String url=""+MainActivity.url+"act=goods&op=find_mobile_email&member_id="+ MainActivity.myLogin.getMemberid()+"";
//        String url = "http://www.xilingou.com/mobile/index.php?act=goods&op=find_mobile_email&member_id="+id+"";
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String resultinfo = responseInfo.result;
//                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");
//                            toast(String.valueOf(result));
                            if (result == 200) {
                                JSONObject recordObject = resultObject.optJSONObject("data");
                                String member_email = recordObject.optString("member_email");
                                String member_mobile = recordObject.optString("member_mobile");
                                int member_email_bind = recordObject.optInt("member_email_bind");
                                int member_mobile_bind = recordObject.optInt("member_mobile_bind");

                                if(member_mobile_bind==1){
                                    Intent intent2 = new Intent();
                                    intent2.setClass(AccountSafeActivity.this, EmailCer2Activity.class);
                                    intent2.putExtra("num",member_mobile);
                                    intent2.putExtra("type","手机");
                                    startActivity(intent2);
                                }else if(member_mobile_bind==0) {
                                    nextAct(PhoneCerActivity.class);
                                }

                            } else {
                                toast("当前暂无数据");
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {
                        toast("请求失败");
                    }
                });
    }
    public void nextAct(Class act) {
        Intent intent = new Intent();
        intent.setClass(this, act);
        startActivity(intent);
    }
}
